package com.cyou.dream.rule;

import com.cyou.dream.context.RuleContext;
import com.cyou.dream.core.IRule;
import com.cyou.dream.rule.param.VirtualPhoneParam;

import java.util.Arrays;


public class VirtualPhoneRule implements IRule {

    // 虚拟号码段
    private static final String[] VIRTUAL_NUMBER_PREFIXES = {
            "1703", "1705", "1706", "165", "1704", "1707", "1708", "1709", "171", "167", "1700", "1701", "1702"
    };

    @Override
    public boolean evaluate(RuleContext context) {

        VirtualPhoneParam valueParams = (VirtualPhoneParam) context.getValueParams();

        if(valueParams == null) {
            throw new IllegalArgumentException("VirtualPhoneRule need value params");
        }

        return !isVirtualNumber(valueParams.getPhone());

    }

    /**
     * 检测手机号是否为虚拟号码
     * @param phoneNumber 手机号
     * @return 如果是虚拟号码返回true，否则返回false
     */
    public static boolean isVirtualNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return false;
        }

        return Arrays.stream(VIRTUAL_NUMBER_PREFIXES).anyMatch(phoneNumber::startsWith);
    }
}
